//
//  MainViewController.swift
//  WeiBo
//
//  Created by 佘红响 on 15/11/6.
//  Copyright © 2015年 she. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        setup()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.addSubview(composeButton)
        
        let width = tabBar.frame.width / CGFloat((tabBar.items?.count)!)
        let height  = composeButton.bounds.height
        let rect = CGRect(x: 2 * width, y: 0, width: width, height: height)
        composeButton.frame = rect
        composeButton.center = CGPointMake(tabBar.frame.width * 0.5, tabBar.frame.height * 0.5)
        
    }
    
    // MARK: - 懒加载
    private lazy var composeButton: UIButton = {
        
        let btn = UIButton.init(imageName: "tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
//        btn.setImage(UIImage(named: "tabbar_compose_icon_add")!, forState: UIControlState.Normal)
//        btn.setImage(UIImage(named: "tabbar_compose_button_highlighted")!, forState: UIControlState.Highlighted)
//        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button")!, forState: UIControlState.Normal)
//        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted")!, forState: UIControlState.Highlighted)
        
        btn.addTarget(self, action: Selector("buttonClick"), forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.sizeToFit()
        
        return btn;
    }()
    
    
    
    // MARK: - 内部控制方法
    // 如果给一个方法加上private, 那么这个方法就只能在当前文件中访问
    // private不光能够修饰方法, 还可以修改类/属性
    
    // 注意: 按钮的点击事件是由运行循环动态派发的, 而如果将方法标记为私有的, 那这个方法在外界就拿不到, 所以动态派发时就找不到该方法
    // 只要加上@objc, 那么就可以让私有方法支持动态派发
    @objc private func buttonClick() {
        HXLog("点击了我")
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    /*
    /**
    初始化操作
    */
    func setup {
        //        // 设置首页
        //        addChildViewController(HomeViewController(), imageName: "tabbar_home", title: "首页")
        //        // 设置消息
        //        addChildViewController(MessageViewController(), imageName: "tabbar_message_center", title: "消息")
        //        // 设置广场
        //        addChildViewController(DiscoverViewController(), imageName: "tabbar_discover", title: "广场")
        //        // 设置我
        //        addChildViewController(ProfileViewController(), imageName: "tabbar_profile", title: "我")
        
        let resuorceName = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)!
        let data = NSData(contentsOfFile: resuorceName)!
        
        guard let objc = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) else {
        HXLog("json文件中没有数据")
        // 设置首页
        addChildViewController("HomeViewController", imageName: "tabbar_home", title: "首页")
        // 设置消息
        addChildViewController("MessageViewController", imageName: "tabbar_message_center", title: "消息")
        // 设置广场
        addChildViewController("DiscoverViewController", imageName: "tabbar_discover", title: "广场")
        // 设置我
        addChildViewController("ProfileViewController", imageName: "tabbar_profile", title: "我")
        return
        }
        
        for dict in objc as! [[String : AnyObject]] {
        addChildViewController(dict["vcName"] as! String, imageName: dict["imageName"] as! String, title: dict["title"] as! String)
        }
    }
    */
    
    
    /*
    /**
    为tabBarControllert添加一个子控制器
    
    :param: childController 显示在导航控制器上的子控制器
    :param: imageName       未选中状态下的图片
    :param: title           导航栏和tabBar上显示的标题
    */
    func addChildViewController(childControllerName: String, imageName: String, title: String) {
        
        // 从info.plist取出命名空间的名
        guard let className = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as? String else {
            
            HXLog("没有获取到命名空间")
            return
        }
        
        // 拼接成类名字符串,转化成可选类型的类
        let cls: AnyClass? = NSClassFromString(className + "." + "childControllerName")
        
        // 解包成确定类型的类
        guard let vcls = cls as? UITableViewController.Type else {
            
            HXLog("cls没有值")
            
            return
        }
        
        // 通过类来创建对象
        let childController = vcls.init()
        
        // 给控制器设置标题
        childController.title = title
        
        // 给控制器设置未选中状态下的图片
        childController.tabBarItem.image = UIImage(named: imageName)

        //
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        
        let navVc = UINavigationController(rootViewController: childController);
        
        addChildViewController(navVc)
    }
    */
    
}
